/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.movieSectionManipulation;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import model.MovieSection;

/**
 *
 * @author Administrator
 */
public class MovieSectionSorting {

    public void sortMovieSection(List<MovieSection> list, String sortBy) {
        Comparator comparator;
        if (sortBy.equals("Movie Name")) {
            comparator = new byMovieName();
        } else if (sortBy.equals("Price")) {
            comparator = new byPrice();
        } else if (sortBy.equals("Start Time")) {
            comparator = new byStartTime();
        } else if (sortBy.equals("Duration")) {
            comparator = new byDuration();
        } else if (sortBy.equals("House")) {
            comparator = new byHouse();
        } else {
            comparator = new byMovieName();
        }
        Collections.sort(list, comparator);
    }

    public static String[] allSortBys() {
        String[] result = {"Movie Name", "Price", "Start Time", "Duration", "House"};
        return result;
    }

    private MovieSectionSorting() {
    }
    private static MovieSectionSorting instance;

    public static MovieSectionSorting getInstance() {
        if (instance == null) {
            instance = new MovieSectionSorting();
        }

        return instance;
    }

    /**
     * Comparator classes
     */
    private class byMovieName implements Comparator {

        @Override
        public int compare(Object obj1, Object obj2) {
            MovieSection ms1 = (MovieSection) obj1;
            MovieSection ms2 = (MovieSection) obj2;
            return ms1.getMovieName().compareToIgnoreCase(ms2.getMovieName());
        }
    }

    private class byPrice implements Comparator {

        @Override
        public int compare(Object obj1, Object obj2) {
            MovieSection ms1 = (MovieSection) obj1;
            MovieSection ms2 = (MovieSection) obj2;
            return ms1.getPrice() - ms2.getPrice();
        }
    }

    private class byStartTime implements Comparator {

        @Override
        public int compare(Object obj1, Object obj2) {
            MovieSection ms1 = (MovieSection) obj1;
            MovieSection ms2 = (MovieSection) obj2;
            return ms1.getStartTime().compareTo(ms2.getStartTime());
        }
    }

    private class byDuration implements Comparator {

        @Override
        public int compare(Object obj1, Object obj2) {
            MovieSection ms1 = (MovieSection) obj1;
            MovieSection ms2 = (MovieSection) obj2;
            return ms1.getDuration() - ms2.getDuration();
        }
    }

    private class byHouse implements Comparator {

        @Override
        public int compare(Object obj1, Object obj2) {
            MovieSection ms1 = (MovieSection) obj1;
            MovieSection ms2 = (MovieSection) obj2;
            return ms1.getHouseId().compareToIgnoreCase(ms2.getHouseId());
        }
    }
}
